﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using AspNetCs.Model;
using AspNetCs.IDAL;
using AspNetCs.Common;


namespace AspNetCs.SQLServerDAL
{
    public class MusicDAL:IMusic
    {

        public DataTable GetDataTable(string filter)
        {
            DataTable dataTable = new DataTable();
            string cmdText = "select *from Music where {0}";
            cmdText = string.Format(cmdText, filter);
            db dbclass = new db();
            dataTable = dbclass.GetSql_DataTable(cmdText);
            return dataTable;
        }


        public MusicModel GetModel(DataRow row)
        {
            MusicModel model = new MusicModel();
            model.Id =StringHelper.isNumber_int(row["Id"].ToString());
            model.MusicName = row["MusicName"].ToString();
            model.MusicPath = row["MusicPath"].ToString();
            model.MusicType = StringHelper.isNumber_int(row["MusicType"].ToString());
            return model;
        }



        /// <summary>
        /// 查询所有的音乐
        /// </summary>
        /// <returns></returns>
        public DataTable queryAllMusic()
        {
            string cmdText = "select *from Music";
            db dbclass = new db();
            DataTable dataTable = dbclass.GetSql_DataTable(cmdText);
            return dataTable;
        }

        /// <summary>
        /// 添加音乐
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddMusic(MusicModel model)
        {
            int count = 0;
            string cmdText = "insert into Music values (@MusicName,@MusicPath,@MusicType,@CreateId,@AddTime)";
            db.ParameterArry[] paras = new db.ParameterArry[5];
            paras[0].dbType = SqlDbType.VarChar;
            paras[0].parName = "@MusicName";
            paras[0].objValue = model.MusicName;

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].parName = "@MusicPath";
            paras[1].objValue = model.MusicPath;

            paras[2].dbType = SqlDbType.Int;
            paras[2].parName = "@MusicType";
            paras[2].objValue = model.MusicType;

            paras[3].dbType = SqlDbType.Int;
            paras[3].parName = "@CreateId";
            paras[3].objValue = model.CreateId;

            paras[4].dbType = SqlDbType.DateTime;
            paras[4].parName = "@AddTime";
            paras[4].objValue = model.AddTime;

            db dbclass = new db();

            count = dbclass.ExcSql(cmdText,paras);


            return count;
        }

        /// <summary>
        /// 修改音乐
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateMusic(MusicModel model)
        {
            int count = 0;
            string cmdText = "update Music set MusicName=@MusicName,MusicPath=@MusicPath,MusicType=@MusicType where id=@id";
            db.ParameterArry[] paras = new db.ParameterArry[4];
            paras[0].dbType = SqlDbType.VarChar;
            paras[0].parName = "@MusicName";
            paras[0].objValue = model.MusicName;

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].parName = "@MusicPath";
            paras[1].objValue = model.MusicPath;

            paras[2].dbType = SqlDbType.Int;
            paras[2].parName = "@MusicType";
            paras[2].objValue = model.MusicType;

            paras[3].dbType = SqlDbType.Int;
            paras[3].parName = "@id";
            paras[3].objValue = model.Id;

            db dbclass = new db();

            count = dbclass.ExcSql(cmdText, paras);


            return count;
        }

        /// <summary>
        /// 删除音乐
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int DeleteMusic(int Id)
        {
            int count = 0;
            string cmdText = "delete from Music where id=@Id";
            db.ParameterArry[] paras = new db.ParameterArry[1];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@Id";
            paras[0].objValue =Id;

            db dbclass = new db();

            count = dbclass.ExcSql(cmdText, paras);


            return count;
        }

    }
}
